package com.gao.weblog.web.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gao.weblog.common.domain.dos.ArticleCategoryRelDO;
import com.gao.weblog.common.domain.dos.ArticleDO;
import com.gao.weblog.common.domain.dos.CategoryDO;
import com.gao.weblog.common.domain.mapper.ArticleCategoryRelMapper;
import com.gao.weblog.common.domain.mapper.ArticleMapper;
import com.gao.weblog.common.domain.mapper.CategoryMapper;
import com.gao.weblog.common.enums.ResponseCodeEnum;
import com.gao.weblog.common.exception.BizException;
import com.gao.weblog.common.utils.PageResponse;
import com.gao.weblog.common.utils.Response;
import com.gao.weblog.web.convert.ArticleConvert;
import com.gao.weblog.web.module.vo.category.FindCategoryArticleListReqVO;
import com.gao.weblog.web.module.vo.category.FindCategoryArticleListRspVO;
import com.gao.weblog.web.module.vo.category.FindCategoryListRspVO;
import com.gao.weblog.web.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper  categoryMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleCategoryRelMapper articleCategoryRelMapper;

    @Override
    public Response findCategoryList() {
        List<CategoryDO> categoryDOS = categoryMapper.selectList(null);
        List<FindCategoryListRspVO> vos = null;

        vos = categoryDOS.stream().map(categoryDO ->
            FindCategoryListRspVO.builder()
                    .id(categoryDO.getId())
                    .name(categoryDO.getName())
                    .build())
                .collect(Collectors.toList());

        return Response.success(vos);
    }

    @Override
    public PageResponse findCategoryArticleList(FindCategoryArticleListReqVO  findCategoryArticleListReqVO) {
        Long categoryId =  findCategoryArticleListReqVO.getId();

        CategoryDO categoryDO = categoryMapper.selectById(categoryId);

        if (Objects.isNull(categoryDO)) {
            log.warn("==> 该分类不存在, categoryId: {}", categoryId);
            throw new BizException(ResponseCodeEnum.CATEGORY_NOT_EXISTED);
        }

        List<ArticleCategoryRelDO> articleCategoryRelDOS = articleCategoryRelMapper.selectByCategoryId(categoryId);

        if (CollectionUtils.isEmpty(articleCategoryRelDOS)) {
            log.info("==> 该分类下还未发布任何文章，categoryId: {}", categoryId);
            return PageResponse.success(null, null);
        }
        List<Long> articleIds = articleCategoryRelDOS.stream()
                .map(ArticleCategoryRelDO::getArticleId).collect(Collectors.toList());

        Long current = findCategoryArticleListReqVO.getCurrent();
        Long size = findCategoryArticleListReqVO.getSize();

        Page<ArticleDO> articleDOPage = articleMapper.selectPageListByArticleIds(current, size, articleIds);
        List<ArticleDO> articleDOS = articleDOPage.getRecords();

        List<FindCategoryArticleListRspVO> vos = null;
        if (!CollectionUtils.isEmpty(articleDOS)) {
            vos = articleDOS.stream().map(articleDO ->
                    ArticleConvert.INSTANCE.convertDO2CategoryArticleVO(articleDO))
                    .collect(Collectors.toList());
        }

        return PageResponse.success(articleDOPage, vos);


    }
}
